package sn_gm_report

import java.text.SimpleDateFormat

class StageWipProfileController {

    def dynamicQueryService
    def commonDataService

    def stageWipProfile() {

        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def todayStart = params.todayStart ? sdf.parse(params.selectTime) : sdf.parse(sdf.format(new Date().clearTime()))

        String strDate = sdf.format(new Date());
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        params.DTN = params.DTN ?: '1'
        if (params.DTN == '2') {

            if (strDateH < 8) {
                //����1��
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.set(Calendar.DAY_OF_MONTH, 1);
                ca.set(Calendar.HOUR_OF_DAY, 8);
                startTime = ca.getTime();
                //����8��
                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(todayStart);
                ca1.add(Calendar.HOUR_OF_DAY, -40);
                todayStart = ca1.getTime();

                Calendar ca2 = Calendar.getInstance();
                ca2.setTime(endTime);
                ca2.add(Calendar.HOUR_OF_DAY, -16);
                endTime = ca2.getTime();

            } else {
                //����1��
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.set(Calendar.DAY_OF_MONTH, 1);
                ca.set(Calendar.HOUR_OF_DAY, 8);
                startTime = ca.getTime();
                //����8��
                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(todayStart);
                ca1.add(Calendar.HOUR_OF_DAY, -16);
                todayStart = ca1.getTime();

                Calendar ca2 = Calendar.getInstance();
                ca2.setTime(endTime);
                ca2.add(Calendar.HOUR_OF_DAY, 8);
                endTime = ca2.getTime();
            }
        } else {
            if (strDateH < 8) {
                Calendar ca = Calendar.getInstance();
                ca.setTime(todayStart);
                ca.add(Calendar.HOUR_OF_DAY, -16);
                todayStart = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, 8);
                endTime = ca1.getTime();

                Calendar ca2 = Calendar.getInstance();
                ca2.setTime(startTime);
                ca2.set(Calendar.DAY_OF_MONTH, 1);
                ca2.set(Calendar.HOUR_OF_DAY, 8);
                startTime = ca.getTime();
            } else {
                Calendar ca = Calendar.getInstance();
                ca.setTime(todayStart);
                ca.add(Calendar.HOUR_OF_DAY, 8);
                todayStart = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, 32);
                endTime = ca1.getTime();

                Calendar ca2 = Calendar.getInstance();
                ca2.setTime(startTime);
                ca2.set(Calendar.DAY_OF_MONTH, 1);
                ca2.set(Calendar.HOUR_OF_DAY, 8);
                startTime = ca2.getTime();
            }
        }
        //�����ڸ�ʽת��Ϊ�ַ�
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        params.todayStart = sdf.format(todayStart)

        def partList = commonDataService.partList(params)
        def materiaType = commonDataService.materiaType(params)

        params.SYS_QUERY_NAME = 'stageWipProfileQty'
        def stageWipProfileQty = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'stageShip'
        def stageShip = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'totalWip'
        def totalWip = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'totalWipHis'
        def totalWipCout = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'stageShipHis'
        def stageShipHis = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'stageWipProfile'
        def stageWipProfile = dynamicQueryService.queryForList(params)

        //������Ŀ�꣬����
        params.SYS_QUERY_NAME = 'lastSumWipList'
        def lastSumWipList = dynamicQueryService.queryForList(params)

        int sumWip
        int lastWip
        if (stageShip) {
            lastWip = stageShip[0].LASTWIP
        }

        def lastStage
        for (int j = 0; j < lastSumWipList.size(); j++) {
            sumWip += lastSumWipList[j].LASTWIP
            lastStage = lastSumWipList[j].STAGE

            if (lastWip <= sumWip) {
                lastStage
                break
            }
        }
        params.lastStage = lastStage

        /**acc.wip**/
        int sum
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i <= stageWipProfileQty.size() - 1; i++) {
            //sum����Ϊ0

            sum += stageWipProfileQty[i].ACCWIP

            String str = sum.toString();
            list.add(i, str)
        }
        Collections.reverse(list)
        print params

        if (params.DTN == '2') {
            [startTime      : params.startTime ?: '', endTime: params.endTime ?: '', DTN: params.DTN ?: '', totalWip: totalWipCout,
             todayStart     : params.todayStart ?: '', stageWipProfileQty: stageWipProfileQty, materiaType: materiaType, lastStage: lastStage,
             partList       : partList, name: params.name ?: '', materia: params.materia ?: '', list: list, stageShip: stageShipHis,
             stageWipProfile: stageWipProfile]
        } else {
            [startTime      : params.startTime ?: '', endTime: params.endTime ?: '', DTN: params.DTN ?: '', totalWip: totalWip,
             todayStart     : params.todayStart ?: '', stageWipProfileQty: stageWipProfileQty, materiaType: materiaType, lastStage: lastStage,
             partList       : partList, name: params.name ?: '', materia: params.materia ?: '', list: list, stageShip: stageShip, lastStage: params.lastStage,
             stageWipProfile: stageWipProfile]
        }
    }

    def stageWipDetails() {
        params.SYS_QUERY_NAME = 'stageWipDetails'
        def stageWipDetails = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (stageWipDetails.size() > 0) {
            tableHeadList = stageWipDetails[0].keySet()
        }
        print stageWipDetails
        print params
        [stageWipDetails: stageWipDetails, tableHeadList: tableHeadList, stage: params.stage ?: '', name: params.name ?: '', materia: params.materia ?: '']
    }

    def test() {
        params.startTime = '2017-11-02 08'
        params.endtime = '2017-11-03 08'
        params.daytime = '2017-11-02 08'

        params.SYS_QUERY_NAME = 'test'
        print params
        def stageWipProfile = dynamicQueryService.queryForList(params)

        print(stageWipProfile)

    }
}
